
/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.inspur.edp.cef.generator.coreGenerator.base.dualaccgen.method;

import com.inspur.edp.caf.generator.baseInfo.AnnotationInfo;
import com.inspur.edp.cef.designtime.api.element.GspElementDataType;
import com.inspur.edp.cef.generator.common.MediatePropertyInfo;
import com.inspur.edp.cef.generator.coreGenerator.base.entitygen.EntityGeneratorContext;
import com.inspur.edp.cef.generator.coreGenerator.base.entitygen.property.BaseTypePropGenerator;
import com.inspur.edp.cef.generator.overall.GeneratorUtil;
import com.inspur.edp.cef.generator.repository.RepositoryGenUtil;
import java.util.ArrayList;
import org.eclipse.jdt.core.dom.Block;
import org.eclipse.jdt.core.dom.Modifier;
import org.eclipse.jdt.core.dom.ReturnStatement;
import org.eclipse.jdt.core.dom.SuperMethodInvocation;

public class DualAccNormalPropGenerator extends BaseTypePropGenerator {

  protected String valueVarName = "value";

  public DualAccNormalPropGenerator(EntityGeneratorContext parContext, String nodeCode,
      MediatePropertyInfo propertyInfo) {
    this(parContext, nodeCode, propertyInfo, false);
  }

  public DualAccNormalPropGenerator(EntityGeneratorContext parContext, String nodeCode,
      MediatePropertyInfo propertyInfo, boolean convertToUpperCase) {
    super(propertyInfo, null, convertToUpperCase);
  }

  @Override
  protected ArrayList<Modifier.ModifierKeyword> getGetterAccessModifier() {
    return RepositoryGenUtil.createModifierKeywordArray(Modifier.ModifierKeyword.PUBLIC_KEYWORD);
  }

//	@Override
//	protected java.lang.Class getExplicitInterfaceType() =>expilcityType;

  /*	get
  {
    return this.InnerData.get_sUdt02Asso();
  }
  }*/
  @Override
  protected Block buildGetMethodBody() {
    ReturnStatement returnStatement = ast.newReturnStatement();
    String methodName = privatePropertyInfo.getGetterMethodName();
    returnStatement.setExpression(RepositoryGenUtil.createMethodInvocation(ast,
        RepositoryGenUtil.createMethodInvocation(ast, null, "getInnerData", null), methodName,
        null));
    Block block = ast.newBlock();
    block.statements().add(returnStatement);
    return block;
  }

  /*	set
    {
      base.RaisePropertyChanging("sUdt02Asso", value, this.InnerData.get_sUdt02Asso());
      bool flag = value == this.InnerData.get_sUdt02Asso();
      if (!flag)
      {
        base.TryCopy();
        this.InnerData.set_sUdt02Asso(value);
      }
    }*/
  @Override
  protected Block buildSetMethodBody() {
    Block block = ast.newBlock();
    addBeforeExtendStatements(block);

    final String methodName = "setAccNormalValue";
    SuperMethodInvocation methodInvocation = ast.newSuperMethodInvocation();
    methodInvocation.arguments()
        .add(RepositoryGenUtil.createStringLiteral(ast, privatePropertyInfo.getPropertyName()));
    methodInvocation.arguments().add(RepositoryGenUtil.createVariableLiteral(ast, valueVarName));
    methodInvocation.setName(ast.newSimpleName(methodName));
    block.statements().add(ast.newExpressionStatement(methodInvocation));

    return block;
  }

  protected void addBeforeExtendStatements(Block block) {
  }

  @Override
  protected ArrayList<AnnotationInfo> getGetterAttributeList() {
    ArrayList<AnnotationInfo> list = super.getGetterAttributeList();
    if (list == null) {
      list = new ArrayList<>();
    }
    if(privatePropertyInfo.getElement().getMDataType() == GspElementDataType.DateTime) {
			list.add(GeneratorUtil.getBefDateTimeDeserializerAnnoInfo(ast));
		} else if(privatePropertyInfo.getElement().getMDataType() == GspElementDataType.Date) {
			list.add(GeneratorUtil.getBefDateDeserializerAnnoInfo(ast));
		}
    return list;
  }

  @Override
  protected ArrayList<AnnotationInfo> getSetterAttributeList() {
    ArrayList<AnnotationInfo> list = super.getSetterAttributeList();
    if (list == null) {
      list = new ArrayList<>();
    }
		if(privatePropertyInfo.getElement().getMDataType() == GspElementDataType.DateTime) {
			list.add(GeneratorUtil.getBefDateTimeSerializerAnnoInfo(ast));
		} else if(privatePropertyInfo.getElement().getMDataType() == GspElementDataType.Date) {
			list.add(GeneratorUtil.getBefDateSerializerAnnoInfo(ast));
		}
    return list;
  }
}
